//index.js
//获取应用实例
const app = getApp();

var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
const db = wx.cloud.database({});
const citys = db.collection('eas-china-citys');

var Indexs
Page({

  data: {
    choosePro: '', //下拉选框选中的省份
    city: '',
    latitude: '',
    longitude: '',
    shows: false, //控制下拉列表的显示隐藏，false隐藏、true显示
    selectDatas: [
      '全国', '北京市', '天津市', '上海市', '重庆市',
      '河北省', '山西省', '辽宁省', '吉林省', '黑龙江省', '江苏省', '浙江省', '安徽省',
      '福建省', '江西省', '山东省', '河南省', '湖北省', '湖南省', '广东省', '海南省',
      '四川省', '贵州省', '云南省', '陕西省', '甘肃省', '青海省', '台湾省',
      '内蒙古自治区', '广西壮族自治区', '西藏自治区', '宁夏回族自治区', '新疆维吾尔自治区',
      '澳门特别行政区', '香港特别行政区',
    ], //下拉列表的数据
    indexs: 0, //选择的下拉列 表下标,
    count: 0, //设置的检查是否获取位置的值
    setProvince: '',
    content:app.globalData.content,
  },

  // 点击下拉显示框
  selectTaps() {
    this.setData({
      shows: !this.data.shows,
    });

  },
  // 点击下拉列表
  optionTaps(e) {
    var _this = this;
    //引用数据库
    const db = wx.cloud.database({});
    const cont = db.collection('eas-yjxx');
    let Indexs = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
    getApp().globalData.index = Indexs;
    console.log(Indexs)
    this.setData({
      indexs: Indexs,
      shows: !this.data.shows,

    });
    const city = this.data.selectDatas
    const choosePro = city[Indexs]
    console.log(choosePro)
    if (choosePro == '全国') {
      cont.skip(0).limit(6).get({
        success: res => {
          this.setData({
            list: res.data,
          })
          console.log('成功22')
          console.log(Indexs)
        }
      })
      cont.skip(0).limit(1).get({
        success: res => {
          this.setData({
            list1: res.data,
          })
        }
      })
      
    } else {
      cont.where({

        content: db.RegExp({
          regexp: choosePro,
          options: 'i',

        })

      }).skip(0).limit(6).get({
        //如果查询成功的话
        success: res => {
          this.setData({
            list: res.data,
          })

          console.log(Indexs)
        }
      })
      cont.where({

        content: db.RegExp({
          regexp: choosePro,
          options: 'i',

        })

      }).skip(0).limit(1).get({
        //如果查询成功的话
        success: res => {
          this.setData({
            list1: res.data
          })
          console.log('成功29')
        }
      })
    }


  },



  onLoad: function () {
    qqmapsdk = new QQMapWX({
      key: 'V7BBZ-FB5RK-GWUJN-AWROS-VNSXO-SXFET'
    });
    getApp().globalData.bindtap1 = 1;
    let that = this;
    getApp().watch(that.watchBack)
  },
  watchBack: function (getProvince){
    console.log(22222);
    console.log('this.getProvince==' + getProvince)

    const city = this.data.selectDatas
    const index=app.globalData.index
    const cont=db.collection('eas-yjxx')
      cont.where({

        content: db.RegExp({
          regexp: app.globalData.getProvince,
          options: 'i',

        })

      }).skip(0).limit(6).get({
        //如果查询成功的话
        success: res => {
          this.setData({
            list: res.data,
            list1:res.data[0]
          })
          console.log('成功23')
        }
      })
      cont.where({

        content: db.RegExp({
          regexp: app.globalData.getProvince,
          options: 'i',

        })

      }).skip(0).limit(1).get({
        //如果查询成功的话
        success: res => {
          this.setData({
            list1: res.data
          })
          console.log('成功29')
        }
      })
    
  },
  onShow: function () {
    var _this = this;
    //引用数据库
    const db = wx.cloud.database({});
    const cont = db.collection('eas-yjxx');
    cont.skip(0).limit(6).get({
      //如果查询成功的话
      success: res => {
        this.setData({
          list: res.data
        })
        console.log('成功')
      }
    })
    cont.skip(0).limit(1).get({
      //如果查询成功的话
      success: res => {
        this.setData({
          list1: res.data
        })
        console.log('成功29')
      }
    })
  

  },
  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res))
        if (res.authSetting['scope.userLocation'] == undefined && res.authSetting['scope.userLocation'] == true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        } else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        // console.log('成功'+JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        // console.log(JSON.stringify(res));
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        vm.setData({
          province: province,
          city: city,
          latitude: latitude,
          longitude: longitude
        })
        const location = res
        const setProvince = province
        console.log(setProvince + 26)
        getApp().globalData.getProvince = setProvince
        console.log(app.globalData.getProvince+27)
        vm.setData({
          selectDatas: [
            setProvince,
            '全国', '北京市', '天津市', '上海市', '重庆市',
            '河北省', '山西省', '辽宁省', '吉林省', '黑龙江省', '江苏省', '浙江省', '安徽省',
            '福建省', '江西省', '山东省', '河南省', '湖北省', '湖南省', '广东省', '海南省',
            '四川省', '贵州省', '云南省', '陕西省', '甘肃省', '青海省', '台湾省',
            '内蒙古自治区', '广西壮族自治区', '西藏自治区', '宁夏回族自治区', '新疆维吾尔自治区',
            '澳门特别行政区', '香港特别行政区',
          ],
        })
      },
      fail: function (res) {
        console.log('错误' + res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },


  //分页按钮
  xuanye1: function () {
    getApp().globalData.bindtap1 = 1;
    const index = app.globalData.index
    var _this = this;
    //引用数据库
    const db = wx.cloud.database({});
    const cont = db.collection('eas-yjxx');
    console.log(index)
    this.setData({
      indexs: index,

    });
    const city = this.data.selectDatas
    const choosePro = city[index]
    console.log(choosePro)
    if (index == 0) {
      cont.skip(0).limit(6).get({
        success: res => {
          this.setData({
            list: res.data
          })
          console.log('成功22')
          console.log(Index)
        }
      })
    } else {
      cont.where({

        content: db.RegExp({
          regexp: choosePro,
          options: 'i',

        })

      }).skip(0).limit(6).get({
        //如果查询成功的话
        success: res => {
          this.setData({
            list: res.data
          })
          console.log('成功25')
          console.log(Indexs)
        }
      })
    }
  },
  xuanye2: function () {
    const db = wx.cloud.database({});
    const cont = db.collection('eas-yjxx');
    getApp().globalData.bindtap1 = 2;
    cont.skip(6).limit(6).get({
      //如果查询成功的话
      success: res => {
        this.setData({
          list: res.data,
        })
      }
    })
  },
  xuanye3: function () {
    const db = wx.cloud.database({});
    const cont = db.collection('eas-yjxx');
    getApp().globalData.bindtap1 = 3;
    cont.skip(12).limit(6).get({
      //如果查询成功的话
      success: res => {
        this.setData({
          list: res.data
        })
      }
    })
  },

  xuanye4: function () {
    const db = wx.cloud.database({});
    const cont = db.collection('eas-yjxx');
    getApp().globalData.bindtap = getApp().globalData.bindtap + 1;
    const bindtap = app.globalData.bindtap;
    cont.skip(bindtap * 6).limit(6).get({
        //如果查询成功的话
        success: res => {
          this.setData({
            list: res.data
          })
        }
      })
  },

})